abstract class Flyweight {
    public abstract doOperation(otherState: string): void
}

class ConcreateFlyweight extends Flyweight {
    //共享的属性
    private myState: string

    constructor(myState: string) {
        super()
        this.myState = myState
    }

    public doOperation(otherState: string): void {
        console.log(`具体享元角色，内部状态为 ${this.myState}, 外部状态为 ${otherState}`);
    }
}


class FlyweightFactory {
    private flyweightStorage: any
    constructor() {
        this.flyweightStorage = {}
    }

    public createFlyweight(myState: string): Flyweight {
        //判断是否是已有属性，如果不是则添加享元，如果已有则直接返回
        if (!this.flyweightStorage[myState]) {
            const flyweight: Flyweight = new ConcreateFlyweight(myState)
            this.flyweightStorage[myState] = flyweight
        }
        return this.flyweightStorage[myState]
    }
}

const FlyweightTest = (): void => {
    const factory : FlyweightFactory = new FlyweightFactory()
    const flyweight1 : Flyweight = factory.createFlyweight("我是内部状态1")
    const flyweight2 : Flyweight = factory.createFlyweight("我是内部状态2")
    flyweight1.doOperation("我是外部状态1")
    flyweight2.doOperation('我是外部状态2')
}

FlyweightTest()